
/**   
*    
* 项目名称：a_yongan   
* 类名称：OutsourcingInController   
* 类描述：   
* 创建人：liubo   
* 创建时间：2018年6月19日 下午3:29:51    
* 修改时间：2018年6月19日 下午3:29:51   
* 修改备注：   
* @version    
*    
*/
package com.yongan.purchase.web;

import java.util.List;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.yongan.purchase.entity.Purchase;
import com.yongan.purchase.entity.PurchaseIn;
import com.yongan.purchase.service.PurchaseInService;
import com.yongan.purchase.service.PurchaseService;
import com.yongan.user.dto.UserResult;
import com.yongan.util.Execution;
import com.yongan.util.PageBean;
import com.yongan.util.SerialNumber;

/**
 * @author liubo
 *
 */
@Controller
@RequestMapping("/purchasein")
public class PurchaseInController {

	@Autowired
	private PurchaseInService purchaseInService;
	@Autowired
	private PurchaseService purchaseService;
	
	@RequestMapping(value = "/add", method = RequestMethod.GET)
	public String purchaseadd(Model model,HttpServletRequest request) {
		String number = "RK"+SerialNumber.Getnum();
		model.addAttribute("number", number);
		
		List<Purchase> purchaselist = null;
		String page = request.getParameter("currentPage");
		
		String dialog = request.getParameter("dialog");//为选择订货单单独增加
		System.out.println(dialog+"=============");
		int currentPage;
		int totalRecords;
		if (page != null) {
			currentPage = Integer.parseInt(page);
		} else {
			currentPage = 1;
		}
		PageBean<Purchase> pb = new PageBean<Purchase>();
		
		totalRecords = purchaseService.getCount();
		pb.setCurrentPage(currentPage);
		pb.setTotalRecords(totalRecords);
		pb.setTotalPages(pb.getTotalRecords() % pb.getPagesize() == 0 ? pb
				.getTotalRecords() / pb.getPagesize() : pb.getTotalRecords()
				/ pb.getPagesize() + 1);
		if (totalRecords > 0) {
			int start = (pb.getCurrentPage() - 1) * pb.getPagesize();
			int end = pb.getCurrentPage() * pb.getPagesize();
			if (end > totalRecords - 1) {
				end = totalRecords;
			}
			purchaselist = purchaseService.getPurchaseList(start, end);
			//model.addAttribute("pb", pb);
		}
		model.addAttribute("pb", pb);
		model.addAttribute("purchaselist", purchaselist);
		
		return "purchaseinadd";
	}
	@RequestMapping(value = "/adddo", method = RequestMethod.POST, produces = { "application/json;charset=UTF-8" })
	@ResponseBody
	public UserResult<Execution> purchaseinadddo(PurchaseIn purchasein,Model model) {
		System.out.println(purchasein.toString());
		purchasein.setPurchaseId(UUID.randomUUID().toString());
		Execution rs = purchaseInService.executePurchaseIn(purchasein, "", "1233213");
		int rsn = rs.getState();
		if (rsn == 1) {
			return new UserResult<Execution>(true, rs);
		}
		return new UserResult<Execution>(false, "");
	}
	
	@RequestMapping(value = "/purchaseinlist", method = RequestMethod.GET)
	public String purchaseincontent(Model model,HttpServletRequest request) {
		List<PurchaseIn> purchaseinlist = null;
		String page = request.getParameter("currentPage");
		int currentPage;
		int totalRecords;
		if (page != null) {
			currentPage = Integer.parseInt(page);
		} else {
			currentPage = 1;
		}
		PageBean<PurchaseIn> pb = new PageBean<PurchaseIn>();
		
		totalRecords = purchaseInService.getCount();
		pb.setCurrentPage(currentPage);
		pb.setTotalRecords(totalRecords);
		pb.setTotalPages(pb.getTotalRecords() % pb.getPagesize() == 0 ? pb
				.getTotalRecords() / pb.getPagesize() : pb.getTotalRecords()
				/ pb.getPagesize() + 1);
		if (totalRecords > 0) {
			int start = (pb.getCurrentPage() - 1) * pb.getPagesize();
			int end = pb.getCurrentPage() * pb.getPagesize();
			if (end > totalRecords - 1) {
				end = totalRecords;
			}
			purchaseinlist = purchaseInService.getPurchaseInList(start, end);
			//model.addAttribute("pb", pb);
		}
		model.addAttribute("pb", pb);
		model.addAttribute("purchaseinlist", purchaseinlist);
		return "purchaseinlist";
	}
	
	@RequestMapping(value = "/{purchaseinId}/detail", method = RequestMethod.GET)
	public String purchasedetail(@PathVariable("purchaseinId") String purchaseinId, Model model) {
		if (!(purchaseinId.equals("")|purchaseinId==null)) {
			PurchaseIn purchasein = purchaseInService.getPurchaseInById(purchaseinId);
			model.addAttribute("purchasein", purchasein);
		}
		return "purchaseindetail";
	}
	
	@RequestMapping(value = "/update", method = RequestMethod.POST, produces = { "application/json;charset=UTF-8" })
	@ResponseBody
	public UserResult<Execution> purchaseinupdate(PurchaseIn purchasein, Model model) {
		Execution rs = purchaseInService.executePurchaseIn(purchasein, purchasein.getPurchaseId(), "123321231");
		int rsn = rs.getState();
		if (rsn == 1) {
			return new UserResult<Execution>(true, rs);
		}
		return new UserResult<Execution>(false, "");
	}
	
	@RequestMapping(value = "/{purchaseinId}/delete", method = RequestMethod.GET, produces = { "application/json;charset=UTF-8" })
	@ResponseBody
	public UserResult<Execution> purchaseinDelete(
			@PathVariable("purchaseinId") String purchaseinId,Model model) {
		if (!(purchaseinId.equals("")|purchaseinId==null)) {
			Execution rsdelete = purchaseInService.executePurchaseIn(null, purchaseinId, "1212331232");
			int rsn = rsdelete.getState();
			if (rsn == 1) {
				return new UserResult<Execution>(true, rsdelete);
			}
		}
		return new UserResult<Execution>(false, "");
	}
	
	@RequestMapping(value = "/{purchaseId}/value", method = RequestMethod.GET, produces = { "application/json;charset=UTF-8" })
	@ResponseBody
	public UserResult<Purchase> purchaseinvalue(
			@PathVariable("purchaseId") String purchaseId, Model model) {
		if (!(purchaseId.equals("")|purchaseId==null)) {
			Purchase purchase = purchaseService.getPurchaseById(purchaseId);
			if (purchase != null) {
				return new UserResult<Purchase>(true, purchase);
			}
		}
		return new UserResult<Purchase>(false, "");
	}
}
